package org.example.managementsystem.repository;

import org.example.managementsystem.model.DeviceRepairReview;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

/**
 * 报修审核记录数据访问对象
 * @author 王振祎
 * @date 2025-05-21
 */
@Repository
public class DeviceRepairReviewDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public DeviceRepairReviewDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 插入报修审核记录
     * @param review 报修审核记录
     * @return 插入的记录的 id
     */
    public int insertReview(DeviceRepairReview review) {
        String sql = "INSERT INTO device_repair_approval (repair_id, is_approved, description) " +
                "VALUES (?, ?, ?)";

        jdbcTemplate.update(sql, 
                review.getRepairId(), 
                review.getIsApproved(), 
                review.getDescription());

        // 获取刚刚插入的 id
        String getLastIdSql = "SELECT LAST_INSERT_ID()";
        int lastInsertId = jdbcTemplate.queryForObject(getLastIdSql, Integer.class);
        return lastInsertId;
    }

    /**
     * 更新报修审核记录
     * @param review 报修审核记录
     */
    public void updateReview(DeviceRepairReview review) {
        String sql = "UPDATE device_repair_approval SET is_approved = ?, description = ?, reviewer_job_number = ? " +
                "WHERE repair_id = ?";
        jdbcTemplate.update(sql, 
                review.getIsApproved(), 
                review.getDescription(), 
                review.getReviewerJobNumber(),
                review.getRepairId());
    }

    public void updateReview(int repairId, boolean isApproved, String description, String reviewerJobNumber) {
        String sql = "UPDATE device_repair_approval SET is_approved = ?, description = ?, reviewer_job_number = ? " +
                "WHERE repair_id = ?";
        jdbcTemplate.update(sql, isApproved, description, reviewerJobNumber, repairId);
    }

    /**
     * 获取所有报修审核记录
     * @return
     */
    public List<DeviceRepairReview> getAllReviews(){
        String sql = "SELECT * FROM device_repair_approval";
        return jdbcTemplate.query(sql, new DeviceRepairReviewRowMapper());
    }

    /**
     * 根据报修ID获取报修审核记录
     * @param repairId 报修ID
     * @return 报修审核记录
     */
    public DeviceRepairReview getReviewByRepairId(int repairId) {
        String sql = "SELECT * FROM device_repair_approval WHERE repair_id = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new DeviceRepairReviewRowMapper(), repairId);
        } catch (EmptyResultDataAccessException e) {
            return null; // 返回空
        }
    }

    /**
     * 报修审核记录映射器
     */
    private static class DeviceRepairReviewRowMapper implements RowMapper<DeviceRepairReview> {
        @Override
        public DeviceRepairReview mapRow(ResultSet rs, int rowNum) throws SQLException {
            DeviceRepairReview review = new DeviceRepairReview();
            review.setRepairId(rs.getInt("repair_id"));
            review.setIsApproved(rs.getBoolean("is_approved"));
            review.setDescription(rs.getString("description"));
            review.setReviewerJobNumber(rs.getString("reviewer_job_number"));
            
            // 处理审核时间字段
            try {
                Timestamp reviewTime = rs.getTimestamp("review_time");
                if (reviewTime != null) {
                    review.setReviewTime(reviewTime.toLocalDateTime());
                }
            } catch (SQLException e) {
                // 如果时间字段不存在，忽略异常
            }
            
            return review;
        }
    }
}